package com.chuangshu.chuangshuteam.controller;


import com.chuangshu.chuangshuteam.annotation.RequiresRoles;
import com.chuangshu.chuangshuteam.dto.AssessmentQuestionsDTO;
import com.chuangshu.chuangshuteam.dto.Result;
import com.chuangshu.chuangshuteam.entity.BusinessKey;
import com.chuangshu.chuangshuteam.service.AssessmentQuestionsService;
import com.chuangshu.chuangshuteam.vo.AssessmentQuestionsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/assessment-questions")
public class AssessmentQuestionsController {

    @Autowired
     AssessmentQuestionsService assessmentQuestionsService;

    @RequiresRoles("member")
    @PostMapping("/add/questions")
    public Result addQuestionsByRecruitmentCyclesId(@RequestBody AssessmentQuestionsVO assessmentQuestionsVO){
        log.info("用户添加面试题 {}", assessmentQuestionsVO);
        if (assessmentQuestionsVO.getTitle().isEmpty()||assessmentQuestionsVO.getDescription().isEmpty()){
            return Result.fail("请填写标题和内容");
        }
        if (assessmentQuestionsVO.getRoundNumber()<=0){
            return Result.fail("请填写正确的面试轮数");
        }
        if (assessmentQuestionsVO.getRecruitmentCycleId()==null||assessmentQuestionsVO.getRecruitmentCycleId()<=0){
            return Result.fail("请填写正确的招聘周期ID");
        }
        String direction = assessmentQuestionsVO.getDirection();
        if (!BusinessKey.isValidDirection(direction)) {
            return Result.fail("请填写正确的方向");
        }

        assessmentQuestionsService.addQuestionsByRecruitmentCyclesId(assessmentQuestionsVO);

        return Result.ok("添加成功");

    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("获取所有面试题");
        Result byPage = assessmentQuestionsService.getByPage(page, pageSize);

        return byPage;

    }
    @GetMapping("/query/questions/{recruitmentCyclesId}")
    public Result getQuestionsByRecruitmentCyclesId(@PathVariable Long recruitmentCyclesId){
        log.info("通过招聘周期获取面试题 {}",recruitmentCyclesId);

        List<AssessmentQuestionsDTO> questionsByRecruitmentCyclesId = assessmentQuestionsService.getQuestionsByRecruitmentCyclesId(recruitmentCyclesId);

        return Result.ok(questionsByRecruitmentCyclesId);

    }

    @GetMapping("/query/detail/{id}")
    public Result getQuestionById(@PathVariable Long id){
        log.info("通过面试题ID获取面试题 {}",id);

        AssessmentQuestionsDTO questionById = assessmentQuestionsService.queryById(id);

        return Result.ok(questionById);

    }
    @RequiresRoles("member")
    @PutMapping("/update")
    public Result updateQuestions(@RequestBody AssessmentQuestionsVO assessmentQuestionsVO) {
    log.info("更新面试题 {}", assessmentQuestionsVO);
    Result result = assessmentQuestionsService.updateQuestions(assessmentQuestionsVO);
    return result;
}
    @RequiresRoles("member")
@DeleteMapping("/delete/{id}")
public Result deleteQuestions(@PathVariable Long id) {
    log.info("删除面试题 ID: {}", id);
    Result result = assessmentQuestionsService.deleteQuestionsById(id);
    return result;
}




}

